package edu.bit.water.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.entity.Column;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import edu.bit.water.dao.MetaWaterDao;
import edu.bit.water.entity.MetaWater;

@Service
@Component
public class MetaWaterService {
    @Autowired
    public MetaWaterDao metaWaterDao;
    @Autowired
    public MetaWaterAssociationService metaWaterAssociationService;
    @Autowired
    public MetaWaterService metaWaterService;
    private boolean flag = false, flagcount = false; // 用来记录实体表字段是否发生改变，true为已改变，false为改变,flag标记所有字段，flagcount标记计算字段
    private List<Column> list = null,listcount = null;

    /**
     * @return 返回所有字段
     */
    public List<Column> selectColumn() {
        if (list == null || flag) {
            list = metaWaterDao.selectColumn();
            flag = false;
        }
        return list;
    }
    /**
     * @return 返回可计算字段
     */
    public List<Column> selectColumnCount() {
    	 if (listcount == null || flagcount) {
             listcount = metaWaterDao.selectColumnCount();
             flagcount = false;
         }
         return listcount;
    }


    /**
     * @return 返回list
     */
    public ArrayList<MetaWater> queryMetaWater() {
        ArrayList<MetaWater> list = metaWaterDao.queryMetaWater();

        return list;
    }

    /**
     * @param metaWater 元数据设备实体类
     * @return 成功返回1，失败返回0
     */
    public Map<String, Object> insertMetaWater(MetaWater metaWater) {
        Map<String, Object> modelMap = new HashMap<>();

        try {
            metaWaterDao.insertMetaWater(metaWater);
            metaWaterAssociationService.insertMetaWaterAssociation(metaWater);
            modelMap.put("status", "success");
            flag = flagcount=true;																//将改动标志flag置为true
        } catch (BadSqlGrammarException e) {
            metaWaterService.deleteMetaWater(metaWater.getEnName());
            modelMap.put("status", "failed");
            modelMap.put("msg", "数据格式出错"+"\n具体报错:"+e.getMessage());
        } catch (DuplicateKeyException e) {
            modelMap.put("status", "failed");
            modelMap.put("msg", "违反唯一性约束"+"\n具体报错:"+e.getMessage());
        } catch (Exception e) {
        	metaWaterService.deleteMetaWater(metaWater.getEnName());
            modelMap.put("status", "failed");
            modelMap.put("msg", "error class:" + e.getClass()+"\n具体报错:"+e.getMessage());
        }
        return modelMap;
    }

    /**
     * @param enName 字段英文名称
     * @return 成功返回1，失败返回0
     */
    public int deleteMetaWater(String enName) {
        int n = metaWaterDao.deleteMetaWater(enName);
        flag =flagcount= true;

        return n;
    }

}
